<!DOCTYPE html>
<html height100 xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta th:replace="authority/common/currency/head"/>
</head>

<body class="childrenBody" padding10>

<div id="seach-container"></div>
<table id="dml-table" lay-filter="dml-table" class="layui-hide zsa-auto-height"></table>

<script type="text/html" id="sys_toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="sys_toolbar_search">搜索</button>
    <button class="layui-btn layui-btn-sm" lay-event="sys_toolbar_refresh">
        <i class="layui-icon" style="line-height:30px">&#xe669;</i>
    </button>
    <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="sys_toolbar_add">
        <i class="layui-icon" style="line-height:30px">&#xe654;</i>添加
    </button>
</script>
<!--操作-->
<script type="text/html" id="toolDemo">
    {{# if(d.taskState==='NONE'||d.taskState==='null'||d.taskState===''||d.taskState==null){ }}
    <a class="layui-btn layui-btn-xs" lay-event="task-start"><i class="layui-icon">&#xe652;</i>启动</a>
    {{# }else if(d.taskState==='PAUSED'){ }}
    <a class="layui-btn layui-btn-xs layui-bg-blue layui-btn-danger" lay-event="task-resume"><i class="layui-icon">&#xe652;</i>继续</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="task-stop"><i class="layui-icon">&#x1007;</i>停止</a>
    {{# }else if(d.taskState==='NORMAL'){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger layui-bg-orange" lay-event="task-pause"><i class="layui-icon">&#xe651;</i>暂停</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="task-stop"><i class="layui-icon">&#x1007;</i>停止</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs {{d.taskState==='NONE'||d.taskState==='null'||d.taskState===''||d.taskState==null?'':'layui-disabled'}}" lay-event="{{d.taskState==='NONE'||d.taskState==='null'||d.taskState===''||d.taskState==null?'edit':''}}"><i class="layui-icon">&#xe642;</i>编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
</script>

<script type="text/html" id="toolSateTemp">
    <span class="layui-badge layui-bg-gray {{d.taskState==='NONE'||d.taskState==='null'?'':'layui-hide'}}">未启动</span>
    <span class="layui-badge layui-bg-green {{d.taskState==='NORMAL'?'':'layui-hide'}}">正常</span>
    <span class="layui-badge layui-bg-orange {{d.taskState==='PAUSED'?'':'layui-hide'}}">暂停</span>
    <span class="layui-badge layui-bg-blue {{d.taskState==='COMPLETE'?'':'layui-hide'}}">完成</span>
    <span class="layui-badge {{d.taskState==='ERROR'?'':'layui-hide'}}">错误</span>
    <span class="layui-badge layui-bg-cyan {{d.taskState==='BLOCKED'?'':'layui-hide'}}">锁定</span>
</script>

<script th:replace="authority/common/currency/footer"></script>
<script>
    layui.extend({
        complexSearch: 'complexSearch/complexSearch', // {/}的意思即代表采用自有路径，即不跟随 base 路径
        initTable: 'initTable/initTable'
    });
    layui.use(['initTable', 'complexSearch', 'lutil', 'initPage'], function () {
        var table = layui.table
            , complexSearch = layui.complexSearch
            , layer = layui.layer
            , initPage = layui.initPage
            , lutil = layui.lutil;
        var search, initData = {};

        initPage.render({successCallback: initPageSuccess}).addFunction(initTaskTypeData).start();

        function initTaskTypeData(functionSuccess) {
            ajaxRequestByCsrf(content_path + "authority/dic/list.do", {parentId: "794E6DE044654C0A90247946F37BD3FA"}, function (res) {
                initData['task_type_options'] = [];
                for (var i = 0; i < res.data.length; i++) {
                    initData['task_type_options'].push({key: res.data[i]['name'], value: res.data[i]['id']});
                }
            }, null, function () {
                functionSuccess();
            })
        }

        function initPageSuccess() {
            // 搜索框
            search = complexSearch.render({
                container: "#seach-container",
                isDynamic: false,
                submit: false,
                returnFormat: {
                    dataTypeName: 'type'
                },
                data: [
                    // {field: 'id', title: 'ID', editType: 'text', dataType: 'string', formulas: ['<->']},
                    {field: 'name', title: '任务名称', editType: 'text', dataType: 'string', formulas: ['<->']},
                    {
                        field: 'taskType',
                        formulas: ['=', 'in', 'notin'],
                        formula: '=',
                        title: '任务类型',
                        dataType: 'int',
                        editType: 'select',
                        options: initData['task_type_options']
                    }, {
                        field: 'modifyTime',
                        title: '最后修改时间',
                        editType: 'date',
                        dateType: 'datetime',
                        dataType: 'date',
                        formulas: ['range', '=', '>', '<'],
                        formula: 'range'
                    },
                    {
                        field: 'taskState',
                        formulas: ['='],
                        dataType: 'int',
                        title: '状态',
                        editType: 'select',
                        options: [{key: '请选择', value: ''}, {key: '未启动', value: 'NONE'}
                            , {key: '正常', value: 'NORMAL'}, {key: '暂停', value: 'PAUSED'}
                            , {key: '完成', value: 'COMPLETE'}, {key: '错误', value: 'ERROR'}, {
                                key: '锁定',
                                value: 'BLOCKED'
                            }]
                    }
                ],
                search: function (data) {
                    var thisTable = layui.pageObtainTable('dml-table');
                    thisTable && thisTable.query({searchs: data.length > 0 ? JSON.stringify(data) : ''});
                }
            });


            var height = $(window).height() - $('#seach-container').outerHeight() - 40,
                height_value = parseInt(height) > 300 ? parseInt(height) : 300;

            layui.page_table_examples.push(table.render({
                elem: '#dml-table',
                url: content_path + 'authority/timed_task/list.do',
                height: height_value,
                toolbar: '#sys_toolbarDemo',
                title: '任务表',
                cellMinWidth: '100',
                headers: {'[[${_csrf.headerName}]]': '[[${_csrf.token}]]'},
                cols: [
                    [//标题栏
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', fixed: 'left', hide: true, width: 100},
                        {field: 'name', title: '任务名称', fixed: 'left', width: 150},
                        {field: 'taskTypeName', title: '任务类型', sort: true, width: 100},
                        {field: 'executionStrategyName', title: '执行策略', sort: true, width: 100},
                        {field: 'cronName', title: 'CRON语句', width: 100},
                        {field: 'taskCron', title: 'CRON表达式', width: 120},
                        {field: 'taskStartTime', title: '启动时间', sort: true, width: 160},
                        {field: 'taskPreviousFireTime', title: '上次运行时间', sort: true, width: 160},
                        {field: 'taskNextFireTime', title: '下一次运行时间', sort: true, width: 160},
                        {field: 'dmlId', title: '语句ID', hide: true, width: 150},
                        {field: 'dmlName', title: 'DML语句名称', width: 150},
                        {field: 'remark', title: '备注', minWidth: 200},
                        {field: 'modifyTime', title: '最后修改时间', hide: true, sort: true, width: 160},
                        {field: 'modifyUserName', title: '最后修改人', sort: true, width: 110},
                        {field: 'sort', title: '排序', sort: true, align: 'center', fixed: 'right', width: 70},
                        {
                            field: 'state', title: '默认启动', sort: true, fixed: 'right', align: 'center', width: 100,
                            templet: function (d) {
                                return '<span' + (d.state === 1 ? ' style="color:green;" >是' : 'style="color:#eee;" >否') + '</span>';
                            }
                        }, {
                        field: 'taskState',
                        title: '状态',
                        sort: true,
                        fixed: 'right',
                        align: 'center',
                        width: 80,
                        toolbar: "#toolSateTemp"
                    },
                        {field: 'cz', title: '操作', width: 270, toolbar: "#toolDemo", fixed: 'right', align: 'center'}
                    ]
                ]
            }));
        }

        //头工具栏事件
        table.on('toolbar(dml-table)', function (obj) {
            var thisTable = layui.pageObtainTable('dml-table');
            var checks = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'sys_toolbar_refresh':
                    thisTable && thisTable.refresh();
                    break;
                case  'sys_toolbar_search':
                    search.submit();
                    break;
                case 'sys_toolbar_add':
                    lutil.open_iframe("新增 定时任务", content_path + "authority/timed_task/edit.html", null, null, function () {
                        thisTable && thisTable.refresh();
                    });
                    break;
            }
        });

        table.on('sort(dml-table)', function (obj) {
            var thisTable = layui.pageObtainTable('dml-table');
            thisTable && thisTable.sort(obj);
        });

        table.on('tool(dml-table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var thisTable = layui.pageObtainTable('dml-table');
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (obj.event) {
                case 'edit':
                    lutil.open_iframe("编辑 - " + data['name'], content_path + "authority/timed_task/edit.html?id=" + data['id'], null, null, function () {
                        thisTable && thisTable.refresh();
                    });
                    break;
                case 'del':
                    lutil.my_confirm("确定要删除该定时任务“" + data['name'] + "” ，将不可恢复？？", function () {
                        var loding = layer.load(2, {shade: [0.3, '#eee']});
                        ajaxRequestByCsrf(content_path + "authority/timed_task/del.do", {id: data['id']}, function (res) {
                            lutil.success_msg(res.message);
                            thisTable && thisTable.refresh();
                        }, null, function () {
                            layer.close(loding);
                        })
                    });
                    break;
                case 'task-start':
                case 'task-resume':
                case 'task-pause':
                case 'task-stop':
                    var stateCode = obj.event.replace('task-', ''), loding = layer.load(2, {shade: [0.3, '#eee']});
                    ajaxRequestByCsrf(content_path + "authority/timed_task/change_task_state.do", {
                        id: data['id'],
                        stateCode: stateCode
                    }, function (res) {
                        lutil.success_msg(res.message);
                        thisTable && thisTable.refresh();
                    }, null, function () {
                        layer.close(loding);
                    });
                    break;
            }
        });

    })
</script>
</body>
</html>